package com.supor.aiot.helper;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.android.baseconfig.base.BaseSharedPreferences;
import com.android.baseconfig.common.utils.Logc;
import com.android.baseconfig.common.utils.Util;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.app.SuporApplication;
import com.supor.aiot.common.app.api.UserImpl;
import com.supor.aiot.common.entity.UserAgreementBean;
import com.supor.aiot.ui.privacy.ProtocolActivity;

import java.util.Date;
import java.util.List;

public class PrivacyProtocolHelper {
    public static boolean UPDATE_PROTOCOL = false;
    private final Context context;
    UserAgreementBean userPrivacy = null;
    UserAgreementBean userServer = null;

    public PrivacyProtocolHelper(Context context) {
        this.context = context;
    }

    public void checkUpdate() {
        getUserPrivacyProtocol();
    }

    /**
     * 用户隐私政策
     */
    @SuppressLint("CheckResult")
    private void getUserPrivacyProtocol() {
        UserImpl.getInstance().getUserPrivacyProtocol().subscribe(dataResult -> {
            if (dataResult.isSuccess()) {
                List<UserAgreementBean> list = dataResult.data.rows;
                if (list.size() > 0) {
                    userPrivacy = list.get(0);
                }
            } else {
                Logc.e("getUserPrivacyProtocol: " + dataResult.status);
            }
            getUserServerProtocol();
        }, throwable -> {
            Logc.e("getUserPrivacyProtocol: " + throwable);
            getUserServerProtocol();
        });
    }

    /**
     * 用户服务协议
     */
    @SuppressLint("CheckResult")
    private void getUserServerProtocol() {
        UserImpl.getInstance().getUserServerProtocol().subscribe(dataResult -> {
            if (dataResult.isSuccess()) {
                List<UserAgreementBean> list = dataResult.data.rows;
                if (list.size() > 0) {
                    userServer = list.get(0);
                }
            } else {
                Logc.e("getUserServerProtocol: " + dataResult.status);
            }
            check();
        }, throwable -> {
            Logc.e("getUserServerProtocol: " + throwable);
            check();
        });
    }

    /**
     * 对比本地协议更新时间
     */
    private void check() {
        long userPrivacyTime = 0;
        long userServerTime = 0;
        int userPrivacyNotice = 1;
        int userServerNotice = 1;
        if (userPrivacy != null) {
            userPrivacyNotice = userPrivacy.getIsNotice();
            Date date = Util.parseDate(userPrivacy.getLastUpdate());
            if (date != null) {
                userPrivacyTime = date.getTime();
                Logc.d("userPrivacyTime: " + userPrivacyTime + ", userPrivacyNotice: " + userPrivacyNotice);
            }
        }
        if (userServer != null) {
            userServerNotice = userServer.getIsNotice();
            Date date = Util.parseDate(userServer.getLastUpdate());
            if (date != null) {
                userServerTime = date.getTime();
                Logc.d("userServerTime: " + userServerTime + ", userServerNotice: " + userServerNotice);
            }
        }

        long oldUserPrivacyTime = BaseSharedPreferences.getLong(context,
                Constants.Key.USER_PRIVACY_TIME);
        long oldUserServerTime = BaseSharedPreferences.getLong(context,
                Constants.Key.USER_SERVER_TIME);
        Logc.d("oldUserPrivacyTime: " + oldUserPrivacyTime + ", oldUserServerTime: " + oldUserServerTime);
        if (AppAgent.getCurrentActivity() != null) {
            String clsName =
                    AppAgent.getCurrentActivity().getClass().getSimpleName();
            boolean isAuth = "AuthPrivacyActivity".equalsIgnoreCase(clsName);
            if (isAuth) {
                BaseSharedPreferences.setLong(context, Constants.Key.USER_PRIVACY_TIME,
                        userPrivacyTime);
                BaseSharedPreferences.setLong(context, Constants.Key.USER_SERVER_TIME,
                        userServerTime);
                return;
            }
        }

        boolean isNotice = (userPrivacyNotice == 1 || userServerNotice == 1);

        if (oldUserPrivacyTime != 0 && oldUserServerTime != 0 && isNotice) {
            if (userPrivacyTime > oldUserPrivacyTime || userServerTime > oldUserServerTime) {
                startProtocolActivity(userPrivacyTime, userServerTime);
            }
        } else {
            BaseSharedPreferences.setLong(context, Constants.Key.USER_PRIVACY_TIME,
                    userPrivacyTime);
            BaseSharedPreferences.setLong(context, Constants.Key.USER_SERVER_TIME, userServerTime);
        }
    }

    private void startProtocolActivity(long userPrivacyTime, long userServerTime) {
        Activity current = AppAgent.getCurrentActivity();
        if (current == null) {
            return;
        }
        boolean isLoading = "LoadingPage".equalsIgnoreCase(current.getClass().getSimpleName())
                || "H5LoadingActivity".equalsIgnoreCase(current.getClass().getSimpleName());
        // boolean isH5 = "H5Activity".equalsIgnoreCase(current.getClass().getSimpleName());
        if (isLoading/* || !isH5*/) {
            Logc.d("isLoading: " + isLoading /*+ ", isH5: " + isH5*/);
            SuporApplication.getInstance().mainHandler.postDelayed(()
                    -> startProtocolActivity(userPrivacyTime, userServerTime), 2000);
            return;
        }
        UPDATE_PROTOCOL = true;
        Intent intent = new Intent();
        intent.putExtra(Constants.Key.USER_PRIVACY_TIME, userPrivacyTime);
        intent.putExtra(Constants.Key.USER_SERVER_TIME, userServerTime);
        intent.setClass(current, ProtocolActivity.class);
        current.startActivityForResult(intent, Constants.REQUEST_CODE);
        current.overridePendingTransition(0, 0);
    }
}
